package com.com.xinke.edu.petmall.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.com.xinke.edu.petmall.databinding.ActivityRegisterBinding;
import com.com.xinke.edu.petmall.entity.ResultBody;
import com.com.xinke.edu.petmall.event.RegisterEvent;
import com.com.xinke.edu.petmall.util.RetrofitUtil;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class RegisterActivityActivity extends AppCompatActivity {
    ActivityRegisterBinding binding;
    // 用户类型选项
    private String[] userTypes = {"普通用户", "商家"};

    int role=3;//角色：1-管理员，2-商家，3-普通用户
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityRegisterBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());

        // 创建适配器，将数据与Spinner关联
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_spinner_item,  // 下拉项样式
                userTypes
        );
        // 设置下拉列表的样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // 给Spinner设置适配器
        binding.spinnerUserType.setAdapter(adapter);

        // 设置默认选中第一项（普通用户）
        binding.spinnerUserType.setSelection(0);

        // 监听选择事件
        binding.spinnerUserType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 获取选中的用户类型
//                String selectedType = userTypes[position];
                // 在这里可以根据选择的类型执行不同的逻辑
                if (position == 0) {
                    // 普通用户逻辑
                    role=3;
                } else {
                    // 商家逻辑
                    role=2;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 未选择时的处理（一般不会触发）
            }
        });

        binding.register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });
    }

    public void register(){
        String account = binding.accountEdt.getText().toString();
        String password=binding.passwordEdt.getText().toString();
        String passwordAgan=binding.passwordAganEdt.getText().toString();
        boolean checked=binding.agreementCheckbox.isChecked();
        if(TextUtils.isEmpty(account)){
            Toast.makeText(this, "账号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(password)||TextUtils.isEmpty(passwordAgan)){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(!password.equals(passwordAgan)){
            Toast.makeText(this, "两次密码必须相同", Toast.LENGTH_SHORT).show();
            return;
        }
        if(!checked){
            Toast.makeText(this, "请先同意用户服务协议和隐私协议", Toast.LENGTH_SHORT).show();
            return;
        }
        RetrofitUtil.getApiService().register(account,password,role+"")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResultBody resultBody) {
                        if(resultBody.getCode()==200){
                            Toast.makeText(RegisterActivityActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            EventBus.getDefault().post(new RegisterEvent(account));
                            finish();
                        }else{
                            Toast.makeText(RegisterActivityActivity.this, resultBody.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.i("onError",e.getMessage());
                        Toast.makeText(RegisterActivityActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}